package LeetCode;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/5/23.
//
//https://leetcode.cn/problems/sqrtx/description/
public class x的平方根 {
    public static void main(String[] args) {
        System.out.println(mySqrt(8));
    }
    public static int mySqrt(int x) {
        if(x < 1)return 0;
        if(x < 2)return 1;
        long left = 1,right = x;
        while (left <= right){
            long mid = (left + right) / 2;
            if(mid * mid > x)right = mid - 1;
            else if(mid * mid == x)return (int) mid;
            else left = mid + 1;
        }
        return (int) right;
    }
}
